package com.erp4j.dynamicfield.controller;

import com.erp4j.dynamicfield.entity.BusinessModule;
import com.erp4j.dynamicfield.service.BusinessModuleService;
import com.erp4j.dynamicfield.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/modules")
public class ModuleController {

    @Resource
    private BusinessModuleService moduleService;

    /**
     * 获取所有模块
     */
    @GetMapping
    public Result<List<BusinessModule>> getAllModules() {
        return Result.success(moduleService.list());
    }

    /**
     * 根据编码获取模块
     */
    @GetMapping("/code/{code}")
    public Result<BusinessModule> getModuleByCode(@PathVariable String code) {
        return Result.success(moduleService.getByCode(code));
    }

    /**
     * 添加模块
     */
    @PostMapping
    public Result<BusinessModule> addModule(@RequestBody BusinessModule module) {
        moduleService.save(module);
        return Result.success(module);
    }

    /**
     * 更新模块
     */
    @PutMapping
    public Result<BusinessModule> updateModule(@RequestBody BusinessModule module) {
        moduleService.updateById(module);
        return Result.success(module);
    }

    /**
     * 删除模块
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteModule(@PathVariable Long id) {
        moduleService.removeById(id);
        return Result.success();
    }
}
